package com.qianniu.napi.common.util;

import org.redisson.Redisson;
import org.redisson.api.*;
import org.redisson.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Strings;

import java.util.concurrent.TimeUnit;

/**
 * Created by chenxuebin on 2017/2/24.
 */
public class RedisUtils {

    private volatile static RedissonClient redis;
    private static Logger logger = LoggerFactory.getLogger(RedisUtils.class);

    private RedisUtils() {
    }

    public static RedissonClient instance() {
        if (redis == null||redis.isShutdown()||redis.isShuttingDown()) {

            synchronized (RedisUtils.class) {
                if (redis == null) {
                    redis = getRedission();
                }
            }
        }
        return redis;
    }

    public static RedissonClient getRedission() {

        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
        RedissonClient redis = Redisson.create(config);
        return redis;
    }

    public static String get(String key) {
        RedissonClient redis = RedisUtils.instance();
        RBucket<String> bucket = redis.getBucket(key);
        return bucket.get();
    }

    public static Boolean isExists(String key) {
        RedissonClient redis = RedisUtils.instance();
        RBucket<String> bucket = redis.getBucket(key);
        return bucket.isExists();
    }

    public static Boolean isNullOrEmpty(String key) {
        RedissonClient redis = RedisUtils.instance();
        RBucket<String> bucket = redis.getBucket(key);
        return (!bucket.isExists())|| Strings.isNullOrEmpty(bucket.get());
    }

    public static Boolean isNotNullOrEmpty(String key) {
        RedissonClient redis = RedisUtils.instance();
        RBucket<String> bucket = redis.getBucket(key);
        return (bucket.isExists())&& !Strings.isNullOrEmpty(bucket.get());
    }

    public static void set(String key,String value) {
        RedissonClient redis = RedisUtils.instance();
        RBucket<String> bucket = redis.getBucket(key);
        bucket.set(value);
    }

    public static void setLong(String key,long value) {
        RedissonClient redis = RedisUtils.instance();
        RAtomicLong rlong = redis.getAtomicLong(key);
        rlong.set(value);
    }

    public static long getLong(String key) {
        RedissonClient redis = RedisUtils.instance();
        RAtomicLong rlong = redis.getAtomicLong(key);
        return rlong.get();
    }

    public static RMap<String, String> getRMap(String key) {
        RedissonClient redis = RedisUtils.instance();
        RMap<String, String> rmap = redis.getMap(key);
        return rmap;
    }


//    public static void setLong(String key,long value) {
//        RedissonClient redis = RedisUtils.instance();
//        RAtomicLong rlong = redis.getAtomicLong(key);
//        rlong.set(value);
//    }

    public static void set(String key, String value, long l, TimeUnit tu) {
        RedissonClient redis = RedisUtils.instance();
        RBucket<String> bucket = redis.getBucket(key);
        bucket.set(value,l,tu);
    }

    public static void delete(String key) {
        RedissonClient redis = RedisUtils.instance();
        RBucket<String> bucket = redis.getBucket(key);
        bucket.delete();
    }

    public static RList<String> getList(String key) {
        RedissonClient redis = RedisUtils.instance();
        RList<String> rlist = redis.getList(key);
        return rlist;
    }

    public static void setList(String key,String value) {
        RedissonClient redis = RedisUtils.instance();
        RList<String> rlist = redis.getList(key);
        rlist.add(value);
    }

    public static void deleteList(String key,Object obj) {
        RedissonClient redis = RedisUtils.instance();
        RList<String> rlist = redis.getList(key);
        rlist.remove(obj);
    }

    public static RSet<String> getSet(String key) {
        RedissonClient redis = RedisUtils.instance();
        return redis.getSet(key);
    }

    public static void setSet(String key,String value) {
        RedissonClient redis = RedisUtils.instance();
        RSet<String> rset = redis.getSet(key);
        rset.add(value);
    }

    public static void deleteSet(String key,Object obj) {
        RedissonClient redis = RedisUtils.instance();
        RSet<String> rset = redis.getSet(key);
        rset.remove(obj);
    }

    public static String getMap(String key,String field) {
        RedissonClient redis = RedisUtils.instance();
        RMap<String,String> rmap = redis.getMap(key);
        return rmap.get(field);
    }

    public static RMap<String,String> getMap(String key) {
        RedissonClient redis = RedisUtils.instance();
        RMap<String,String> rmap = redis.getMap(key);
        return rmap;
    }

    public static void setMap(String key,String field,String value) {
        RedissonClient redis = RedisUtils.instance();
        RMap<String,String> rmap = redis.getMap(key);
        String result = rmap.put(field,value);
        logger.debug(">>>setMap key="+key+" field="+field+" value="+value+" result="+result);
    }

    public static void deleteMap(String key,String field) {
        RedissonClient redis = RedisUtils.instance();
        RMap<String,String> rmap = redis.getMap(key);
        rmap.remove(field);
    }

    public static void main(String[] args) {

        RedissonClient redis = RedisUtils.getRedission();
//        RBucket<String> bucket = redis.getBucket("test");
//        bucket.set("ok");
//        System.out.println("bucket test ="+bucket.get());

        RAtomicLong bucket = redis.getAtomicLong("test_atomicLong");
//        bucket.set(3l);
        System.out.println("bucket test =" + bucket.get());
        Long newRepeatDays = 1l;
        newRepeatDays = bucket.incrementAndGet();
        System.out.println("bucket test =" + newRepeatDays);
        newRepeatDays = bucket.incrementAndGet();
        System.out.println("bucket test =" + newRepeatDays);

    }

}
